package com.guodi.model.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* @描述: 时间工具类
* @入参:
* @出参:
* @作者: 代鹏刚
* @日期: 2020/10/23 10:06
**/

public class TimeUtils {

    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
    * @描述: 获取当前年的上一年
    * @入参:
    * @出参:
    * @作者: 代鹏刚
    * @日期: 2020/10/23 10:07
    **/
    public static String getPreYear(){
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR) - 1;
        return year+"";
    }

    /**
    * @描述: 获取当前季度的上一季度
    * @入参:
    * @出参:
    * @作者: 代鹏刚
    * @日期: 2020/10/23 10:07
    **/
    public static String getPreQuarter(){
        Calendar cal = Calendar.getInstance();
        int month = cal.get(Calendar.MONTH) + 1;
        int year = cal.get(Calendar.YEAR);
        int quarter = month % 3 == 0 ? month/3 : month/3 + 1;
        if(quarter == 1){
            return year - 1 + "-4";
        }
        return year + "-" + (quarter - 1);
    }

    /**
    * @描述: 获取当前月份的上一月份
    * @入参:
    * @出参:
    * @作者: 代鹏刚
    * @日期: 2020/10/23 10:08
    **/
    public static String getPreMonth(){
        Calendar cal = Calendar.getInstance();
        int month = cal.get(Calendar.MONTH) + 1;
        int year = cal.get(Calendar.YEAR);
        if(month == 1){
            return year - 1 + "-12";
        }
        month = month - 1;
        String m = month < 10 ? "0" + month : month + "";
        return year + "-" + m;
    }

    /**
    * @描述: 更新不同更新频率获取更新状态
    * @入参:
    * @出参:
    * @作者: 代鹏刚
    * @日期: 2020/10/23 11:21
    **/
    public static String getUpdateStatus(String type){
        if(type.equals("year")){
            Calendar cal = Calendar.getInstance();
            int month = cal.get(Calendar.MONTH) + 1;
            if(month == 1){
                return "1";
            }
            return "2";
        }else if(type.equals("quarter")){
            Calendar cal = Calendar.getInstance();
            int month = cal.get(Calendar.MONTH) + 1;
            int day = cal.get(Calendar.DATE);
            if(month % 3 == 1 && day <= 15){
                return "1";
            }
            return "2";
        }else {
            Calendar cal = Calendar.getInstance();
            int day = cal.get(Calendar.DATE);
            if(day <= 10){
                return "1";
            }
            return "2";
        }
    }

    /**
    * @描述: 更新上报值的更新时间
    * @入参:
    * @出参:
    * @作者: 代鹏刚
    * @日期: 2020/11/7 16:08
    **/
    public static String getReportStatus(String type){
        if(type.equals("year")){
            Calendar cal = Calendar.getInstance();
            int month = cal.get(Calendar.MONTH) + 1;
            int day = cal.get(Calendar.DATE);
            if(day <= 25){
                return "1";
            }else {
                return "2";
            }
        }else {
            return "1";
        }
    }

    public static Boolean verifyUpdateTime(String type, String updateTime){
        String eL = "";
        if(type.equals("year")){
            eL = "[0-9]{4}";
            Pattern p = Pattern.compile(eL);
            Matcher m = p.matcher(updateTime);
            return m.matches();
        }else if(type.equals("month")){
            eL = "[0-9]{4}(\\-)((0[1-9])|(1[0-2]))";
            Pattern p = Pattern.compile(eL);
            Matcher m = p.matcher(updateTime);
            return m.matches();
        }else {
            eL = "[0-9]{4}(\\-)([1-4])";
            Pattern p = Pattern.compile(eL);
            Matcher m = p.matcher(updateTime);
            return m.matches();
        }
    }

    public static String formatDate(Date createTime){
        return simpleDateFormat.format(createTime);
    }
}
